package com.csair.common;

import android.app.Application;
import android.text.TextUtils;

import com.csair.common.helper.Constants;
import com.csair.common.utils.LogUtils;
import com.csair.common.utils.SharedPreferencesUtil;

import java.lang.reflect.Field;

/**
 * @author vaycent (vaycentsun@gmail.com)
 * @date 2018/6/5
 */

public class AppUtilsConfig {
    private static final String TAG = "CommonAppInit";
    private static boolean isDebugMode = false;
    public static final String APP_PACKAGE_NAME = "com.csair.mbp";
    public static String APP_VERSIONNAME="";

    /***
     *  标明运行环境的buildConfig路径，利用反射获取属性：
     *  <p>ISDEBUG_BUILD是否debug模式，初始化LogUtil功能</>
     *  <p>接口URL，触屏URL，URL地址是否debug模式</>
     *  <p>deviceId</>
     */
    private static final String BUILDCONFIG_PATH = "com.csair.mbp.launcher.BuildConfig";
    private static final String BUILD_TYPE_ISDEBUG_KEY = "ISDEBUG_BUILD";
    public static final String DOMAIN_REQUEST_URL_KEY = "LastBaseUrl";
    public static final String DOMAIN_TOUCH_URL_KEY = "LastTouchUrl";
    public static final String URL_IS_RELEASE_ENVIRONMENT = "LastOtherUrlInReleaseEnvironment";
    public static final String DEVICE_ID_DEFAULT = "deviceID";

    /***
     *  用来初始化SharepreferencesUtil功能, 功能开关SP,App配置，
     */
    public static final String PREFERENCE_DEFAULT_FILENAME = "client_preferences";
    public static final String PREFERENCE_FEATURESSWITCH_FILENAME = "features_switch";
    public static final String PREFERENCE_APPCONFIG_FILENAME = "AppConfig";
    /**
     *  App首页地址，登录页地址
     *  用来提供底层基础的页面跳转
     */
    public static final String ACTIVITY_HOME_PATH = "com.csair.mbp.CSMBPActivity";
    public static final String ACTIVITY_LOGIN_PATH = "com.csair.mbp.login.activity.LoginActivity";
    public static final String ACTIVITY_COMMONNEBULA_PATH = "com.csair.mbp.devops.nebula.CommonNebulaActivity";
    public static final String ACTIVITY_HYBRID_PATH = "com.csair.mbp.launcher.hybrid.CommonHybridActivity";



















    /**
     * 用于application初始化配置,工具类
     * @param application application上下文环境
     */
    public static void initWith(Application application){
        long startTime = System.currentTimeMillis();
        resetBuildType();
        resetAppVersion();
        //由于resetDomainUrl中使用了sharePreferencesUtil，所以需要在后面
        SharedPreferencesUtil.initSharedPreferencesUtil(application);
        resetDomainURL();
        LogUtils.e(TAG, "初始化底层工具模块 初始化时间为 " + (System.currentTimeMillis() - startTime) + " ms");
    }

    /***
     * @return 获取目前运行环境是否debug模式
     */
    public static boolean getIsDebugMode(){
        return isDebugMode;
    }



    /**
     * 获取目前App运行环境是否是debug模式,设置到配置中
     */
    private static void resetBuildType(){
        try {
            Class<?> classBuildConfig = Class.forName(AppUtilsConfig.BUILDCONFIG_PATH);
            Object obj = classBuildConfig.newInstance();
            Field buildTypeIsDebug = classBuildConfig.getField(AppUtilsConfig.BUILD_TYPE_ISDEBUG_KEY);
            buildTypeIsDebug.setAccessible(true);
            boolean isDebugValue = (boolean)(buildTypeIsDebug.get(obj));
            if(isDebugValue){
                isDebugMode = true;
            }
        } catch (Throwable e) {
            e.fillInStackTrace();
        }
    }

    /**
     * 获取目前App运行环境中的VersionCode和VersionName
     */
    private static void resetAppVersion(){
        try {
            Class<?> classBuildConfig = Class.forName(AppUtilsConfig.BUILDCONFIG_PATH);
            Object obj = classBuildConfig.newInstance();
            Field buildTypeIsDebug = classBuildConfig.getField("VERSION_NAME");
            buildTypeIsDebug.setAccessible(true);
            String versionName = (String)(buildTypeIsDebug.get(obj));
            if(!TextUtils.isEmpty(versionName)){
                APP_VERSIONNAME = versionName;
            }
        } catch (Throwable e) {
            e.fillInStackTrace();
        }
    }


    /**
     * 获取目前App运行环境中的接口和触屏域名url，若没有则使用默认
     */
    private static void resetDomainURL(){
        try {
            Class<?> classBuildConfig = Class.forName(AppUtilsConfig.BUILDCONFIG_PATH);
            Object obj = classBuildConfig.newInstance();
            Field DomainRequestUrl = classBuildConfig.getField(AppUtilsConfig.DOMAIN_REQUEST_URL_KEY);
            Field DomainTouchUrl = classBuildConfig.getField(AppUtilsConfig.DOMAIN_TOUCH_URL_KEY);
            DomainRequestUrl.setAccessible(true);
            DomainTouchUrl.setAccessible(true);
            String strDomainRequestUrl = (String)(DomainRequestUrl.get(obj));
            String strDomainTouchUrl = (String)(DomainRequestUrl.get(obj));
            Constants.refreshCommonBase(strDomainRequestUrl);
            Constants.refreshTouchBase(strDomainTouchUrl);
        } catch (Throwable e) {
            e.fillInStackTrace();
        }
    }
}
